package com.cakin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: RegexTest
 * @Description: TODO
 * @Date: 2020/6/18
 * @Author: cakin
 */
public class RegexTest {
    public static void main(String[] args) {
        //  .*runoob.* 用于查找字符串中是否包了 runoob 子串
        String pattern = ".*runoob.*";
        String content = "I am runoob!";

        System.out.println(Pattern.matches(pattern, content));
        System.out.println("========================================");

        // 按指定模式在字符串查找
        String line = "This order was placed for QT3000! OK?";
        String pattern1 = "(\\D*)(\\d+)(.*)";

        // 通过正则表达是来创建 Pattern 对象（已经封装好了正则表达式）,
        Pattern r = Pattern.compile(pattern1);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find()) {
            System.out.println("Found value: " + m.group(0));
            System.out.println("Found value: " + m.group(1));
            System.out.println("Found value: " + m.group(2));
            System.out.println("Found value: " + m.group(3));
        } else {
            System.out.println("NO MATCH");
        }
        System.out.println("========================================");
        test();
        System.out.println("========================================");

        test1();


        test2();
        System.out.println("========================================");
        test3();

    }

    public static void test() {
        String REGEX = "\\bcat\\b";
        String INPUT = "cat cat cat cattie cat";

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); // 获取 matcher 对象
        int count = 0;

        while (m.find()) {
            count++;
            System.out.println("Match number " + count);
            System.out.println("start(): " + m.start());
            System.out.println("end(): " + m.end());
        }

    }

    public static void test1() {
        String REGEX = "foo";
        String INPUT = "fooooooooooooooooo";
        String INPUT2 = "ooooofoooooooooooo";
        Pattern pattern;
        Matcher matcher;
        Matcher matcher2;
        // 通用的语法
        // 第一步：compile
        pattern = Pattern.compile(REGEX);
        // 第二步：matcher
        matcher = pattern.matcher(INPUT);
        matcher2 = pattern.matcher(INPUT2);

        System.out.println("Current REGEX is: " + REGEX);
        System.out.println("Current INPUT is: " + INPUT);
        System.out.println("Current INPUT2 is: " + INPUT2);


        System.out.println("lookingAt(): " + matcher.lookingAt());  // true
        System.out.println("matches(): " + matcher.matches()); // false

        System.out.println("lookingAt(): " + matcher2.lookingAt()); // false
        System.out.println("matches(): " + matcher2.matches()); // false

    }

    public static void test2() {
        String REGEX = "dog";
        String INPUT = "The dog says meow. " + "All dogs say meow.";
        String REPLACE = "cat";

        Pattern p = Pattern.compile(REGEX);
        // get a matcher object
        Matcher m = p.matcher(INPUT);
        INPUT = m.replaceFirst(REPLACE);
        System.out.println(INPUT);

    }

    public static void test3() {
        String REGEX = "a*b";
        String INPUT = "aabfooaabfooabfoobkkk";
        String REPLACE = "-";
        Pattern p = Pattern.compile(REGEX);
        // 获取 matcher 对象
        Matcher m = p.matcher(INPUT);
        StringBuffer sb = new StringBuffer();
        // 下面4句的功能，将INPUT正则替换后输出结果，输出结果放到sb中
        while (m.find()) {
            m.appendReplacement(sb, REPLACE);
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    }
}


